import java.util.LinkedList;
import java.util.Queue;

public class Test {

    int[] dx = {0,0,1,-1};
    int[] dy = {1,-1,0,0};
    boolean[][] vis;
    int m,n;







    //被围绕的区域
    public void bfs1(char[][] board,int i,int j){
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{i,j});
        board[i][j] = '.';

        while (!q.isEmpty()){
            int[] t = q.poll();
            int a = t[0] ,b = t[1];
            for (int k = 0; k < 4; k++) {
                int x = a+dx[k], y= b+dy[k];
                if (x>=0 && x<m && y>=0 && y<n && board[x][y] == 'O'){
                    board[x][y] = '.';
                    q.add(new int[]{x,y});
                }
            }
        }
    }

    public void solve(char[][] board){
        m = board.length; n = board[0].length;

        //处理边界的‘O’ 全部改成‘.’
        for (int j = 0; j < n; j++) {
            if (board[0][j] == 'O') bfs1(board,0, j);
            if (board[m-1][j] == 'O') bfs1(board,m-1,j);
        }

        for (int i = 0; i < m; i++) {
            if (board[i][0] == 'O') bfs1(board,i,0);
            if (board[i][n-1] == '0') bfs1(board,i,n-1);
        }

        //还原
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'O') board[i][j] ='X';
                else if(board[i][j] == '.') board[i][j]= 'O';
            }
        }
    }

    public void bfs(char[][] grid, int i, int j) {
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{i, j});
        vis[i][j] = true;

        while (!q.isEmpty()) {
            int[] t = q.poll();
            int a = t[0], b = t[1];
            for (int k = 0; k < 4; k++) {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' && !vis[x][y]) {
                    q.add(new int[]{x, y});
                    vis[x][y] = true;
                }
            }
        }
    }

    public int bfs(int[][] grid,int i,int j){
        Queue<int[]> q = new LinkedList<>();
        int count = 0;
        q.add(new int[]{i,j});
        vis[i][j] = true;
        count++;

        while (!q.isEmpty()){
            int[] t = q.poll();
            int a = t[0], b = t[1];
            for (int k = 0; k < 4; k++) {
                int x = a+dx[k], y= b+dy[k];
                if (x>=0 && x<m && y>=0 && y<n && grid[i][j] == 1 && !vis[i][j]){
                    q.offer(new int[]{x,y});
                    vis[x][y] = true;
                    count++;
                }
            }
        }
        return count;
    }

    //岛屿的最大面积
    public int maxAreaOfIsland(int[][] grid){
        m = grid.length; n = grid[0].length;
        vis = new boolean[51][51];
        int ret = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1 && !vis[i][j]){
                    ret = Math.max(ret,bfs(grid,i,j));
                }
            }
        }
        return ret;
    }







    //岛屿数量
    public int numIslands(char[][] grid){
        m = grid.length;n = grid[0].length;
        vis = new boolean[301][301];
        int ret = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1' && !vis[i][j]){
                    ret++;
                    bfs(grid,i,j);
                }
            }
        }
        return ret;
    }





    //图像渲染
    public int[][] floodFill(int[][] image,int sr,int sc,int color){
        int prev = image[sr][sc];//记录刚开始的颜色

        if(prev == color) return image;

        int m = image.length , n = image[0].length;

        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{sr,sc});

        while(!q.isEmpty()){
            int[] t = q.poll();
            int a = t[0] , b = t[1];

            image[a][b] = color;
            for (int i = 0; i < 4; i++) {
                int x = a+dx[i] , y = b+dy[i];
                if (x>=0 && x<m && y>=0 && y<n && image[x][y] == prev){
                    q.add(new int[]{x,y});
                }
            }
        }
        return image;
    }






}
